/**
 * 
 */
package hr.fer.zemris.rznu.model.authorization;

import hr.fer.zemris.rznu.model.dto.authorization.RoleDTO;
import hr.fer.zemris.rznu.model.factories.ConverterFromDTOFactory;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.NamedQuery;

/**
 * @author Dajan
 *
 */
@Entity
@Table(name="ROLE")
@NamedQueries({
    @NamedQuery(name="roleByName",query="SELECT r FROM Role r WHERE r.name = ?1")
})
public class Role {

	public static enum RoleType{
		ADMIN,
		USER
	}
	
	@Id
	@GeneratedValue
	private Integer id;
	/**
	 * Users that are in this role.
	 */
	@OneToMany(mappedBy="role")
	private Set<UserInRole> usersInRole;
	/**
	 * Role name.
	 */
	@Column(name="name",length=50,unique=true,nullable=false)
	private String name;
	
	@Enumerated
	private RoleType roleType;
	
	public Role() {
		
	}
	
	public Role(RoleDTO roleDTO){
		
		this.usersInRole = ConverterFromDTOFactory.getUsersInRole(roleDTO.getUsersInRoleDTO());
		this.name = roleDTO.getName();
		
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Set<UserInRole> getUsersInRole() {
		return usersInRole;
	}

	public void setUsersInRole(Set<UserInRole> usersInRole) {
		this.usersInRole = usersInRole;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public RoleType getToleType() {
		return roleType;
	}

	public void setToleType(RoleType toleType) {
		this.roleType = toleType;
	}
	
}
